<?php
session_start();
require_once "../includes/common.php";
require_once BASE_PATH.'/class/Explore.class.php';
require_once BASE_PATH.'/class/User.class.php';
require_once "../islogin.php";

class ExploreApi {
    private $explore;


    public function __construct() {
        $this->explore = new Explore();
    }

    public function handleRequest() {
        if (!isset($_GET['type'])) {
            $this->respond(400, ['error' => '缺少 type 参数']);
            return;
        }

        //安全验证uid
        global $user_info; // 使用 global 关键字引用外部变量

        if($_POST['meuid']){
            if ($_POST['meuid']!=$user_info['message']['user_id']) {
                $this->respond(json_encode(['message' => 'uid error!']));
                return;
            }
        }

        if($_POST['uid']&&!$_POST['meuid']){
            if ($user_info['message']['user_id']&&$_POST['uid']!=$user_info['message']['user_id']) {    //修复2024-10-03 登录执行该逻辑
                $this->respond(json_encode(['message' => 'uid error!']));
                return;
            }
        }


        switch ($_GET['type']) {
            case 'random':
                $this->random();
                break;
            case 'prandom':
                $this->prandom();
                break;
            case 'usernote':
                $this->userNote();
                break;
            case 'search':
                $this->search();
                break;
            case 'hidenote':
                $this->hidenote();
                break;
            case 'videonook':
                $this->videonook();
                break;
            case 'videcgok':
                $this->videcgok();
                break;
            case 'reportnote':
                $this->reportnote();
                break;
            case 'userlike':
            case 'userfavorite':
                $this->userLikeNote($_GET['type']);
                break;

            default:
                $this->respond(400, ['error' => '无效的 type 参数']);
                break;
        }
    }

    private function random() {
        $uid = isset($_POST['uid']) ? $_POST['uid'] : '';
        $result = $this->explore->randomNotes(20,$uid);
        return $this->respond($result);
    }

    private function hidenote() {
        $uid = isset($_POST['uid']) ? $_POST['uid'] : '';
        $nid = isset($_POST['nid']) ? $_POST['nid'] : '';
        $result = $this->explore->hideNote($uid,$nid);
        return $this->respond($result);
    }
    private function videonook() {
        $result = $this->explore->getVideoNook();
        return $this->respond($result);
    }
    private function videcgok() {
        $nid = isset($_POST['nid']) ? $_POST['nid'] : '';
        $isok = isset($_POST['isok']) ? $_POST['isok'] : '';
        $filesrc = isset($_POST['filesrc']) ? $_POST['filesrc'] : '';

        $result = $this->explore->VideoChangeok($nid,$isok,$filesrc);
        return $this->respond($result);
    }
    private function reportnote() {
        $nid = isset($_POST['nid']) ? $_POST['nid'] : '';
        $result = $this->explore->reportNote($nid);
        return $this->respond($result);
    }

    private function prandom() {
        $uid = isset($_POST['uid']) ? $_POST['uid'] : '';
        $result = $this->explore->recommendNotes(20,$uid);
        return $this->respond($result);
    }

    private function userNote() {
        $uid = isset($_POST['uid']) ? $_POST['uid'] : '';
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $pageSize = 12;

        $result = $this->explore->profileNotes($uid, $pageSize, $page);

        // 返回响应
        return $this->respond($result);
    }

    private function postnote() {
        $uid = isset($_POST['uid']) ? $_POST['uid'] : '';
        $title = $_POST['title'] ?? '';
        $content = $_POST['content'] ?? '';
        $tags = $_POST['tags'] ?? '';
        $images = $_POST['images'] ?? '';

        $result = $this->explore->postNote($uid, $title, $content, $images, $tags);

        return $this->respond($result);
    }

    private function userLikeNote($type) {
        $uid = isset($_POST['uid']) ? $_POST['uid'] : '';
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $pageSize = 12;
        $type = ($type === 'userfavorite') ? 1 : 0;

        $result = $this->explore->likeNotes($uid, $pageSize, $page,$type);

        // 返回响应
        return $this->respond($result);
    }

    private function search(){
        $note_type = $_POST['note_type'];
        $keyword = isset($_POST['keyword']) ? $_POST['keyword'] : '';
        $page = isset($_POST['page']) ? (int)$_POST['page'] : 1;
        $pageSize = 12; // 每页条数，默认为 12


        $result = $this->explore->searchNotes($note_type, $keyword, $pageSize, $page);;

        // 返回响应
        return $this->respond($result);
    }


    private function respond($data) {
        header('Content-Type: application/json');
        echo $data;
        exit;
    }
}

$api = new ExploreApi();
$api->handleRequest();


